package com.sctvcloud.renhe.ui.activities;

import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.sctvcloud.renhe.R;
import com.sctvcloud.renhe.application.Constances;
import com.sctvcloud.renhe.base.BaseDetailActivity;
import com.sctvcloud.renhe.ui.utils.TitleUtils;
import com.sctvcloud.renhe.ui.widget.MyWebChromeClient;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HelpCenterActivity  extends BaseDetailActivity {
    private static final String serviceUrl = Constances.MALL_ST_BASE_URL+"sctv/scgcmall/help/help.html";
    @BindView(R.id.web_view)
    protected WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new TitleUtils(this).useAndSetTxTitle("帮助中心");
        initView();
    }

    @Override
    protected void initInject() {
        setContentView(R.layout.activity_help_center);
        ButterKnife.bind(this);
    }

    private void initView(){
        WebSettings settings = webView.getSettings();
        // 使用缓存
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        String ua = settings.getUserAgentString();
        settings.setUserAgentString(ua + " SCGC_UA");
//        settings.getUserAgentString()
        //使h5页面适应手机屏幕
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        //设置可以支持缩放
        settings.setSupportZoom(true);
        //设置出现缩放工具
        settings.setBuiltInZoomControls(true);
        //隐藏缩放工具
        settings.setDisplayZoomControls(false);
        //扩大比例的缩放
        settings.setUseWideViewPort(true);
        // 支持JavaScript
        settings.setJavaScriptEnabled(true);
//        settings.setMediaPlaybackRequiresUserGesture(false);
        webView.setWebChromeClient(new MyWebChromeClient() {
            // For Android  > 4.1.1
            public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
//                Log.i("test", "openFileChooser 3");
//                IntegralStoreActivity.this.uploadFile = uploadFile;
//                openFileChooseProcess(null);
            }

        });
        webView.setWebViewClient(new WebViewClient() {
            /**
             * 防止加载网页时调起系统浏览器
             */
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (!url.startsWith("http://")&&!url.startsWith("https://")) {
                    return true;
                }
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                String titleStr = view.getTitle();
                if (!TextUtils.isEmpty(titleStr)) {
//                    if (TextUtils.isEmpty(title)){
//                    title = titleStr;
//                    }
                }
            }
        });

        if (!TextUtils.isEmpty(serviceUrl)) {
            webView.loadUrl(serviceUrl);
        }

    }
}
